package com.duowei.diandan.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.app.InitManager;
import com.duowei.diandan.app.OrderList;
import com.duowei.diandan.constant.ChoiceType;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.databinding.ChoiceFragmentBinding;

/**
 * 堂食、外带选择
 */
public class ChoiceFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "ChoiceFragment";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitManager.getInstance().setMaxCountdownSecond(Settings.COUNTDOWN_SECOND);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ChoiceFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_choice, container, false);

        binding.eatInLayout.setOnClickListener(this);
        binding.takeOutLayout.setOnClickListener(this);
        binding.scanLayout.setOnClickListener(this);
        binding.backButton.setOnClickListener(this);

        if (Settings.SCAN_FUC) {
            binding.scanLayout.setVisibility(View.VISIBLE);
        } else {
            binding.scanLayout.setVisibility(View.GONE);
        }

        return binding.getRoot();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.eatInLayout:
                Settings.CHOICE_TYPE = ChoiceType.EAT_IN;
                OrderList.getInstance().createWmlsbjb();

                if (mOnClickListener != null) {
                    mOnClickListener.gotoMenuFragment();
                }

                break;
            case R.id.takeOutLayout:
                Settings.CHOICE_TYPE = ChoiceType.TAKE_OUT;
                OrderList.getInstance().createWmlsbjb();

                if (mOnClickListener != null) {
                    mOnClickListener.gotoMenuFragment();
                }

                break;
            case R.id.scanLayout:
                Settings.CHOICE_TYPE = ChoiceType.SCAN;
                OrderList.getInstance().createWmlsbjb();

                if (mOnClickListener != null) {
                    mOnClickListener.gotoOrderFragment();
                }

                break;
            case R.id.backButton:
                if (mOnClickListener != null) {
                    mOnClickListener.onBackClick();
                }
                break;
        }
    }

    private OnClickListener mOnClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void gotoMenuFragment();

        void gotoOrderFragment();

        void onBackClick();
    }
}
